import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const my = new MyModel();
const common = new Common();

Page({
  data: {
    expId: null, // 项目经验id
    demoName: null, //项目名称
    entryTime: null, // 开始时间
    quitTime: null, //  结束时间
    position: null, // 承担职位
    projectDesc: null // 项目介绍
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 开始时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 结束时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
    }
  },
  submit(e) {
    let value = e.detail.value;
    if(value) {
      let data = {
        expId: this.data.expId,
        resumeId: common.getRid(),
        beginTime: this.data.entryTime,
        endTime: this.data.quitTime,
        projectName: value.demoName,
        position: value.position,
        projectIntroduce: value.projectDesc
      }
      if(data['projectName'] == null || data['projectName'].trim() == '') {
        wx.showToast({
          title: '项目名称不能为空',
          icon: 'none'
        });
        return false
      }else if(data['beginTime'] == null) {
        wx.showToast({
          title: '开始时间不能为空',
          icon: 'none'
        });
        return false
      }else if(data['endTime'] == null) {
        wx.showToast({
          title: '结束时间不能为空',
          icon: 'none'
        });
        return false
      }else if(data['position'] == null || data['position'].trim() == '') {
        wx.showToast({
          title: '担任职务不能为空',
          icon: 'none'
        });
        return false
      }else if(data['projectIntroduce'] == null || data['projectIntroduce'].trim() == '') {
        wx.showToast({
          title: '项目介绍不能为空',
          icon: 'none'
        });
        return false
      }
      if(data['beginTime'] > data['endTime']) {
        wx.showToast({
          title: '开始时间不能大于结束时间!',
          icon: 'none'
        });
        return false
      }
      console.log(data);
      // 提交
      my.editExperience(data)
      .then(res => {
        if(res.code == 2) {
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000
          });
          setTimeout(() => {
            wx.redirectTo({
              url: '../index/index'
            });
          }, 2000)
        }else {
          wx.showToast({
            title: '修改失败，请稍后重试！',
            icon: 'none',
            duration: 2000
          });
        }
      })
    }
  },
  onLoad: function (options) {
    let expId = options.expid;
    if(expId) {
      this.setData({
        expId: expId
      })
    }
    // 回显项目经验
    my.showExperience(expId)
    .then(res => {
      if(res.code == 2) {
        res = res.data.experience;
        this.setData({
          entryTime: res.beginTime, 
          quitTime: res.endTime,
          demoName: res.projectName,
          position: res.position,
          projectDesc: res.projectIntroduce,
          loading: true
        })
      }
    })
  }
})